<?php

/**
 * class provides functions for the administration of groups within va_stratlab database
 *
 * @author voellenk
 */

class groupspage extends htmlpage {
  public $form;
  public $availablegroups;
  public $showgroup;
  public $showgroupoverview;
  public $currentgroupid;
  public $error;
  public $dbopstring;
  private $mysql;
  private $edit_group;
  private $edit_group_member;
  
  //html elements
  // query strings
  private $qs_edit_group="eg";
  private $qs_edit_group_member="egm";
  private $in_from="in_from";
  private $in_to="in_to";
  private $in_g_code="in_group_code";
  private $in_g_name="in_group_name";
  private $cmd_insert_gm="insert_group_member";
  private $cmd_update_gm="update_group_member";
  private $cmd_delete_gm="delete_group_member";
  private $cmd_insert_g="insert_group";
  private $cmd_update_g="update_group";
  private $cmd_delete_g="delete_group";
  private $in_inserted_id="inserted_sym_id";
  private $in_id="sym_id";
  private $in_gid="group_id";
    
  public function presetFormVariables() {    
    $this->showgroup=false;
    $this->showgroupoverview=true;
    $this->form["group"] = "";
  }
  
  public function initDb($mysql) {
    $this->mysql=$mysql;
  }
    
  public function getGeneralDbInfos() {
    $this->availablegroups = $this->mysql->get_groups();
  }
  
  public function processPostVariable($post) {
    // edit group members
    if (!empty($post) & $this->showgroup) {
      // insert group member
      if ($post[$this->in_id] == "blank" & array_key_exists($this->cmd_insert_gm, $post)) {
        if ($post[$this->cmd_insert_gm]=="insert" & is_numeric($post[$this->in_inserted_id]) & $post[$this->in_inserted_id] > 0) {
          if ($this->mysql->insert_id_into_group($post[$this->in_inserted_id], $this->currentgroupid, 
              $post[$this->in_from], $post[$this->in_to])) {
            $this->dbopstring= "inserted new symbol id ".$post[$this->in_inserted_id]."\n";           
          } else {
            $this->dbopstring= "ERROR while inserting new symbol id ".$post[$this->in_inserted_id]."\n";              
          }

        } else {
          $this->dbopstring= "invalid smy_id ".$post[$this->in_inserted_id]."\n";
        }
      }
      // delete group member
      if (array_key_exists($this->cmd_delete_gm, $post)) {
        if ($post[$this->cmd_delete_gm] == "delete") {
          if ($this->mysql->delete_id_out_of_group($post[$this->in_id], $this->currentgroupid)) {
            $this->dbopstring= "deleted symbol id ".$post[$this->in_id]." out of group\n";
          } else {
            $this->dbopstring= "ERROR while deleting symbol id ".$post[$this->in_id]." out of group\n";            
          }
        }
      }
      // update group member
      if (array_key_exists($this->cmd_update_gm, $post)) {
        if ($post[$this->cmd_update_gm] == "update") {
          if ($this->mysql->update_id_in_group($post[$this->in_id], $this->currentgroupid,
              $post[$this->in_from], $post[$this->in_to])) {
            $this->dbopstring= "updated symbol id ".$post[$this->in_id]."\n";
          } else {
            $this->dbopstring= "ERROR updating symbol id ".$post[$this->in_id]."\n";            
          }
        }
      }
    }
    // edit groups
    if (!empty($post) & $this->showgroupoverview) {
      // insert group
      if (array_key_exists($this->cmd_insert_g, $post)) {
        if (!($post[$this->in_g_code]=="" | $post[$this->in_g_name]=="") &
             $this->mysql->insert_group($post[$this->in_g_code], $post[$this->in_g_name])) {
          $this->dbopstring= "inserted group ".$post[$this->in_g_code]."\n";
        } else {
          $this->dbopstring= "inserted nothing.\n";          
        }
      }
      // delete group
      if (array_key_exists($this->cmd_delete_g, $post)) {
        if ($post[$this->cmd_delete_g] == "delete") {
          if ($this->mysql->delete_group($post[$this->in_gid])) {
            $this->dbopstring= "deleted group id ".$post[$this->in_gid]." out of db\n";            
          } else {
            $this->dbopstring= "ERROR while deleting group id ".$post[$this->in_gid]." out of db\n";                 
          }
        }
      }
      // update groups
      if (array_key_exists($this->cmd_update_g, $post)) {
        if ($post[$this->cmd_update_g] == "update") {
          if ($this->mysql->update_group($post[$this->in_gid], 
              $post[$this->in_g_code], $post[$this->in_g_name])) {
            $this->dbopstring= "updated group id ".$post[$this->in_gid]."\n";           
          } else {
            $this->dbopstring= "ERROR updating group id ".$post[$this->in_gid]."\n";              
          }
        } 
      }
    }
  }
  
  public function parseGetVariable($GET){
    if (array_key_exists ('group',$GET)){
      $gp = urldecode ($GET["group"]);
      $gpex= explode("-", $gp);
      if ($gpex[0] != "") {
        $this->form["group"]=$gp;
        $this->currentgroupid= trim($gpex[0]); // contains the group- id
        $this->showgroup=true;
      } else {
        $this->showgroupoverview=true;
      }
    } else {
      $this->form["group"]="";
      $this->showgroupoverview=true;
    }
    if (array_key_exists ($this->qs_edit_group, $GET)) {
      if (urldecode($GET[$this->qs_edit_group])) $this->edit_group=TRUE;
    }
    if (array_key_exists ($this->qs_edit_group_member, $GET)) {
      if (urldecode($GET[$this->qs_edit_group_member])) $this->edit_group_member=TRUE;
    }
  }

  
  public function titleBar(){
    $html= "<form name='MENU' method='get' action='".$_SERVER["PHP_SELF"]."'>\n";
    $html.= "<table>";

    //Menu Titlebar
    //group
    $html.= "<tr><th>Group</th>\n";

    //link to Main Page
    $html.= "<th><a href ='index.php'>Main Page</a></th>\n";
    $html.="</tr>\n";

    //SELECT group
    $html.= "<tr><td><select name='group' size='1'>\n";
    $html.= "<option> </option>\n";
    foreach ($this->availablegroups as $group) {
      if ($group["id"] == $this->currentgroupid) {
        $html.= "<option selected='selected'>".$group["id"]." - ".$group["code"]."</option>\n";
      } else {
        $html.= "<option>".$group["id"]." - ".$group["code"]."</option>\n";
      }
    }
    $html.= "</select></td>\n";
    
    //GO Button
    $html.= "<td>\n<input type='submit' name='go' value='OK' /></td>\n";
    $html.= "</tr>\n";
    $html.= "</table>\n";
    $html.= "</form>\n";
    $html.= "<hr />\n";
    return htmlpage::addStyle($html, htmlpage::$styleTM);
  }
  
  public function group_edit_bar() {
    $html ="<table><tr><th><a href='".$_SERVER["PHP_SELF"]."?$this->qs_edit_group=1'>
            add/edit group</a></th></tr></table>\n";
    
    return htmlpage::addStyle($html, htmlpage::$styleEB);       
  }
  
  public function group_member_edit_bar() {
    $html ="<table><tr><th><a href='".$_SERVER["PHP_SELF"].
            "?$this->qs_edit_group_member=1&group=".$this->form["group"]."'>
            add/edit group member</a></th></tr></table>\n";
    
    return htmlpage::addStyle($html, htmlpage::$styleEB);       
  }
  
  public function groupoverview() {
    $html ="<h2>groups overview</h2>\n";
    $html.="<table>";
    $html.="<tr><th>id</th><th>code</th><th>name</th><th># symbols</th>";
    if ($this->edit_group) {
      $html.="<th></th>";
    }
    //insert row
    if ($this->edit_group) {
      $html.=$this->g_post_formstring("blank");
      $html.="<tr><td></td>
        <td><input name='$this->in_g_code' type='text' value='' size='20' maxlength='20'></td>
        <td><input name='$this->in_g_name' type='text' value='' size='30' maxlength='100'></td>
        <td></td>
        <td><input type='submit' name='$this->cmd_insert_g' value='insert' /></td></tr>\n";
      $html.="</form>\n";
    }
    $html.="</tr>\n";
    foreach ($this->availablegroups as $group) {
      $html.=$this->g_post_formstring($group["id"]);
      $html.="<tr><td>".$group["id"]."</td>";
      if ($this->edit_group) {
        $html.="<td><input name='$this->in_g_code' type='text' value='".$group["code"]."' size='20' maxlength='20'</td>\n";
        $html.="<td><input name='$this->in_g_name' type='text' value='".$group["name"]."' size='30' maxlength='100'</td>\n";       
      } else {
        $html.="<td>".$group["code"]."</td><td>".$group["name"]."</td>";
      }
      $nomembers=$this->mysql->count_groups($group["id"]);
      $html.="<td>".current($nomembers)."</td>";
      if ($this->edit_group) {
        $html.="<td>";
        $html.="<input type='submit' name='$this->cmd_update_g' value='update' />";
        if (current($nomembers)==0) {         
          $html.="<input type='submit' name='$this->cmd_delete_g' value='delete' />\n";
        }
        $html.="</td>";
      }
      $html.="</tr>\n";
      $html.="</form>\n";
    }
    $html.="</table>\n";
    
    return htmlpage::addStyle($html, htmlpage::$styleOV);   
  }
  
  private function g_post_formstring($name) {
    $string ="<form name='$name' action='".$_SERVER["PHP_SELF"].
              "?$this->qs_edit_group=1' method='post'>\n";
    $string.= "<input type='hidden' name='$this->in_gid' value='$name' />\n";
    return ($string);
  }
  
  private function gm_post_formstring($name) {
    $string ="<form name='$name' action='".$_SERVER["PHP_SELF"].
              "?$this->qs_edit_group_member=1&group=".$this->form["group"]."' method='post'>\n";
    $string.= "<input type='hidden' name='$this->in_id' value='$name' />\n";
    return ($string);
  }
  
  public function groupdetails() {
    $group=$this->mysql->get_group($this->currentgroupid);
    $nomembers=$this->mysql->count_groups($group["id"]);
    if (!current($nomembers)) $this->edit_group_member=TRUE;
    $members=$this->mysql->get_group_symbols($group["id"]);
    
    $html ="<h2>group ".$group["code"]."</h2>\n";
    $html.="<table><tr><th>id</th><th>code</th><th>name</th><th># symbols</th>";
    $html.="<tr><td>".$group["id"]."</td><td>".$group["code"]."</td><td>".$group["name"]."</td>";
    $html.="<td>".current($nomembers)."</td></tr>\n";
    $html.="</table>\n";    
    
    $html.="<h3>symbols in group</h3>\n";
    $html.="<table><tr><th rowspan='2'>id</th><th rowspan='2'>code</th>
      <th rowspan='2'>name</th><th rowspan='2'>exchange</th><th colspan='2'>in db</th><th colspan='2'>use</th>\n";
    if ($this->edit_group_member) {
      $html.="<th rowspan='2'></th>";
    }
    $html.="</tr>\n";
    $html.="<tr><th>from</th><th>to</th><th>from</th><th>to</th></tr>\n";
    // insert- row
    if ($this->edit_group_member) {
      $html.=$this->gm_post_formstring("blank");
      $html.="<tr><td><input name='$this->in_inserted_id' type='text' size='5'></td>
        <td colspan='5'> </td>
        <td><input name='$this->in_from' type='text' value='' size='10' maxlength='10'></td>
        <td><input name='$this->in_to' type='text' value='' size='10' maxlength='10'></td>
        <td><input type='submit' name='$this->cmd_insert_gm' value='insert' /></td></tr>\n";
      $html.="</form>\n";
    }
    foreach ($members as $member) {
      $fromto=$this->mysql->get_date_boundaries($member["sid"]);
      $html.=$this->gm_post_formstring($member["sid"]);
      $html.="<tr><td>".$member["sid"]."</td><td>".$member["code"]."</td><td>".$member["name"]."</td>";
      $html.="<td>".$member["eid"]." - ".$member["ename"]."</td>";
      // date boundaries in db
      $html.="<td>".$fromto[0]." </td><td>".$fromto[1]." </td>";
      // date boundaries as used
      if ($this->edit_group_member) {
        $html.="<td><input name='$this->in_from' type='text' value='".$member["from"]."' size='10' maxlength='10'></td>";
        $html.="<td><input name='$this->in_to' type='text' value='".$member["to"]."' size='10' maxlength='10'></td>";      
      } else {
        $html.="<td>".$member["from"]." </td><td>".$member["to"]." </td>";
      }
      if ($this->edit_group_member) {
        $html.="<td><input type='submit' name='$this->cmd_update_gm' value='update' />
                <input type='submit' name='$this->cmd_delete_gm' value='delete' /></td>";
      }
      $html.="</tr>\n";
      $html.="</form>\n";
    }
    $html.="</table>\n";
    return htmlpage::addStyle($html, htmlpage::$styleOV);   
  }
}

?>
